package com.zyx.crawlerdemo.excelprocessor.crawler;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * @author Yaxi.Zhang
 * @since 2021/8/12 12:07
 * desc: 通过POI写出数据到Excel
 */
public class DataToExcelByPoi {
    public static void writeInfoListToExcel(String filePath,
                                            String sheetName,
											List<PostModel> datalist) throws IOException {
        // 文件名称
        File file = new File(filePath);
        OutputStream outputStream = new FileOutputStream(file);
        Workbook workbook = getWorkBook(file);
        Sheet sheet = workbook.createSheet(sheetName);
        // 添加表头
        // 创建某行
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("post_title");
        row.createCell(1).setCellValue("post_content");
        //添加内容
        for (int i = 0; i < datalist.size(); i++) {
            Row everyRow = sheet.createRow(i + 1);
            everyRow.createCell(0).setCellValue(
                    datalist.get(i).getPostTitle());
            everyRow.createCell(1).setCellValue(
                    datalist.get(i).getPostContent());
        }
        workbook.write(outputStream);
        // 释放资源
        workbook.close();
        outputStream.close();
        System.out.println(">>>>>>>>>数据写入完成！<<<<<<<<<<<<<");
    }

    /**
     * 判断Excel的版本,初始化不同的Workbook
     *
     * @param file Excel文件
     * @return 判断版本后Excel的Workbook
     * @throws IOException 抛出异常
     */
    public static Workbook getWorkBook(File file) throws IOException {
        Workbook workbook = null;
        //Excel 2003
        if (file.getName().endsWith("xls")) {
            workbook = new HSSFWorkbook();
            // Excel 2007以上版本
        } else if (file.getName().endsWith("xlsx")) {
            workbook = new XSSFWorkbook();
        }
        return workbook;
    }
}
